package com.skywolf.chem.comments.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.skywolf.chem.comments.entity.CommentInfo;
import com.skywolf.chem.comments.mapper.CommentInfoMapper;
import com.skywolf.chem.comments.pojo.CommentQueryParam;
import com.skywolf.chem.comments.service.ICommentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 评论列表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-06-09
 */
@Service
public class CommentInfoServiceImpl extends ServiceImpl<CommentInfoMapper, CommentInfo> implements ICommentInfoService {

    @Autowired
    CommentInfoMapper commentInfoMapper;

    @Override
    public IPage<CommentInfo> getCommentList(CommentQueryParam queryParam,String uid) {
        QueryWrapper<CommentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_status", 1).eq("article_id",queryParam.getArticleId()).orderByDesc("created_time");
        if(StringUtils.isNotBlank(uid)){
            queryWrapper.or(wrapper -> wrapper.eq("cus_uid", Long.valueOf(uid))).eq("article_id",queryParam.getArticleId());
        }
        return this.page(queryParam.page(), queryWrapper);
    }

    @Override
    public PageVO<CommentInfo> managerCommentList(CommentQueryParam queryParam) {
        Long startNum = queryParam.getPageSize()*(queryParam.getPageNum()-1);
        Long endNum = queryParam.getPageSize();
        List<CommentInfo> commentList = commentInfoMapper.getCommentPageList(startNum,endNum,
                queryParam.getArticleId(),queryParam.getCommentDesc(),queryParam.getArticleTitle(),queryParam.getStatus(),
                queryParam.getStartTime(),queryParam.getEndTime());
        Integer count = commentInfoMapper.getCommentPageCount(queryParam.getArticleId(),queryParam.getCommentDesc(),queryParam.getArticleTitle(),queryParam.getStatus(),
                queryParam.getStartTime(),queryParam.getEndTime());

        PageVO<CommentInfo> pageVO = new PageVO<>();
        pageVO.setList(commentList);
        pageVO.setTotal(Long.valueOf(count));
        pageVO.setPageSize(queryParam.getPageSize());
        pageVO.setPageNum(queryParam.getPageNum());
        Long totalPage = (count/queryParam.getPageSize())+(count%queryParam.getPageSize()>0?1:0);
        if(totalPage.equals(0)){
            totalPage = queryParam.getPageNum();
        }
        pageVO.setPages(totalPage);
        return pageVO;
    }


    @Override
    public PageVO<CommentInfo> myCommentList(CommentQueryParam queryParam,Long uid) {
        Long startNum = queryParam.getPageSize()*(queryParam.getPageNum()-1);
        Long endNum = queryParam.getPageSize();
        List<CommentInfo> commentList = commentInfoMapper.myCommentPageList(startNum,endNum,uid);
        Integer count = commentInfoMapper.myCommentPageCount(uid);

        PageVO<CommentInfo> pageVO = new PageVO<>();
        pageVO.setList(commentList);
        pageVO.setTotal(Long.valueOf(count));
        pageVO.setPageSize(queryParam.getPageSize());
        pageVO.setPageNum(queryParam.getPageNum());
        Long totalPage = (count/queryParam.getPageSize())+(count%queryParam.getPageSize()>0?1:0);
        if(totalPage.equals(0)){
            totalPage = queryParam.getPageNum();
        }
        pageVO.setPages(totalPage);
        return pageVO;
    }
}
